'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log(event)
	let res = {
		success: false,
		message: '请求失败',
		data: {
			list: [],
		}
	};
	// 选择表
	const yeardb = db.collection('year')
	const monthdb = db.collection('month')
	const daydb = db.collection('day')
	const converdb = db.collection('conver')
	// 根据 object_id 查询年份
	let data_year = await yeardb.where({
		object_id: event._id,
	}).get();
	let listdata = res.data.list;
	for (let i = 0; i < data_year.data.length; i++) {
		console.log(listdata)
		// 这个操作是为目录列表添加数据
		listdata.push({
			id: data_year.data[i]._id,
			name: data_year.data[i].year + data_year.data[i].y_name,
			children: []
		})
		// 根据 object_id 和 year_id 查询这一年的月份
		let data_month = await monthdb.where({
			object_id: event._id,
			year_id: data_year.data[i]._id,
		}).get()
		let m_list = listdata[i].children;
		for (let m = 0; m < data_month.data.length; m++) {
				// 这个操作是为目录列表添加数据
			m_list.push({
				id: data_month.data[m]._id,
				name: data_month.data[m].month + data_month.data[m].m_name,
				children: []
			})
			// 根据 object_id 和 month_id 查询这这月的天数
			let data_day = await daydb.where({
				object_id: event._id,
				month_id: data_month.data[m]._id,
			}).get()
			let day_list = m_list[m].children;
			for (let d = 0; d < data_day.data.length; d++) {
					// 这个操作是为目录列表添加数据
				day_list.push({
					id: data_day.data[d]._id,
					name: data_day.data[d].day + data_day.data[d].d_name+' '+data_day.data[d].title,
					title: data_day.data[d].title
				})
			}
			m_list[m].children = day_list;
		}
		listdata[i].children = m_list;
		/**
		 * 因为不可能每天都写，所以只能去这样查
		 * */
	}
	if (listdata) {
		res = {
			success: true,
			message: '请求成功！',
			code:'0000',
			data: {
				list: listdata,
			}
		}
		return res;
	}

	//返回数据给客户端
	return res;
};
